package cn.com.dcsgo.controller;

import cn.com.dcsgo.md5.MD5;
import cn.com.dcsgo.model.acl.Admin;
import cn.com.dcsgo.result.Result;
import cn.com.dcsgo.service.AdminService;
import cn.com.dcsgo.service.RoleService;
import cn.com.dcsgo.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/*
接口请求路径、请求方式、请求参数参考前端文件：
uu-preferred-front-end/uuyx-admin/src/api/acl/user.js
 */

/**
 * 用户接口
 *
 * @author Dcsgo
 * @since 2025/3/18 16:58
 */
@RestController
@Api(tags = "用户接口")
@RequiredArgsConstructor
@RequestMapping("admin/acl/user")
public class AdminController {
    private final AdminService adminService;
    private final RoleService roleService;

    /**
     * 用户条件分页查询
     *
     * @param page         页码
     * @param limit        分页大小
     * @param adminQueryVo adminQueryVo
     * @return Result->IPage->Admin>
     */
    @ApiOperation("用户条件分页查询")
    @GetMapping("{page}/{limit}")
    public Result<IPage<Admin>> getPageList(@PathVariable Long page,
                                            @PathVariable Integer limit,
                                            AdminQueryVo adminQueryVo) {
        Page<Admin> pageParam = new Page<>(page, limit);
        IPage<Admin> result = adminService.selectAdminPage(pageParam, adminQueryVo);
        return Result.ok(result);
    }

    /**
     * 根据id获取用户信息
     *
     * @param id id
     * @return Result->Admin
     */
    @ApiOperation("根据id获取用户信息")
    @GetMapping("get/{id}")
    public Result<Admin> getById(@PathVariable Long id) {
        return Result.ok(adminService.getById(id));
    }

    /**
     * 添加用户
     *
     * @param admin admin
     * @return Result->null
     */
    @PostMapping("save")
    @ApiOperation("添加用户")
    public Result<Serializable> add(@RequestBody Admin admin) {
        //给密码加密
        String password = admin.getPassword();
        String encrypt = MD5.encrypt(password);
        admin.setPassword(encrypt);

        if (adminService.save(admin)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    /**
     * 通过id修改用户
     *
     * @param admin admin
     * @return Result->null
     */
    @PutMapping("update")
    @ApiOperation("通过id修改用户")
    public Result<Serializable> update(@RequestBody Admin admin) {
        if (adminService.updateById(admin)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    /**
     * 通过id删除用户
     *
     * @param id id
     * @return Result->null
     */
    @ApiOperation("通过id删除用户")
    @DeleteMapping("remove/{id}")
    public Result<Serializable> removeById(@PathVariable Long id) {
        if (adminService.removeById(id)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    /**
     * 批量删除用户
     *
     * @param ids ids
     * @return Result->null
     */
    @ApiOperation("批量删除用户")
    @DeleteMapping("batchRemove")
    public Result<Serializable> removeUsers(@RequestBody List<Long> ids) {
        if (adminService.removeByIds(ids)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }

    /**
     * 获取所有角色及其用户所有角色
     *
     * @param adminId adminId
     * @return Result->Map->allRolesList,assignRoles
     */
    @ApiOperation("获取用户所有角色")
    @GetMapping("toAssign/{adminId}")
    public Result<Map<String, Object>> getRoles(@PathVariable Long adminId) {
        return Result.ok(roleService.selectRolesAndAssigned(adminId));
    }

    /**
     * 为用户分配角色
     *
     * @param adminId adminId
     * @param roleIds roleIds
     * @return null
     */
    @ApiOperation("为用户分配角色")
    @PostMapping("doAssign")
    public Result<Serializable> assignRoles(@RequestParam Long adminId, @RequestParam("roleId") Long[] roleIds) {
        if (roleService.saveAdminRoles(adminId, roleIds)) {
            return Result.ok(null);
        }
        return Result.fail(null);
    }

}
